//
//  HMLoginController.m
//  HMMarket
//
//  Created by 孙鹏飞 on 2017/5/9.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMLoginController.h"
#import "CheckTool.h"
#import "HMForgetController.h"
#import "HMHomeViewController.h"

@interface HMLoginController ()

@property (weak, nonatomic) IBOutlet UITextField *mobileTextField; // 用户名输入框
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField; // 密码输入框
@property (weak, nonatomic) IBOutlet UIButton *loginBtn; // 登录按钮
@property (weak, nonatomic) IBOutlet UIButton *registerBtn; // 注册按钮
@property (weak, nonatomic) IBOutlet UIButton *forgetBtn; // 忘记密码按钮

@end

@implementation HMLoginController

#define kTextColor      [UIColor colorWithHexString:@"000000"] //文字色

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"登录";
    [self setupView];
}

// 登录
- (IBAction)doLogin:(id)sender{
    CATransition * anim = [CATransition animation];
    anim.type = @"kCATransitionMoveIn";
    anim.duration = 0.5;
    [[UIApplication sharedApplication].keyWindow.layer addAnimation:anim forKey:nil];
    
    HMHomeViewController * vc = [[HMHomeViewController alloc] init];
    [UIApplication sharedApplication].keyWindow.rootViewController = vc;
    
    return;
    
    // 检查手机号
    if (![CheckTool checkMobile:self.mobileTextField.text]) {
        return;
    }
    
    // 检查表单
    if (![self checkForm]) {
        return;
    }
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"tel"] = self.mobileTextField.text;
    params[@"passWord"] = self.passwordTextField.text;
    
    [HttpBaseTool post:kLoginUrl params:params success:^(id responseObj) {
        if ([responseObj[@"code"] integerValue]== 200) {
            
            NSArray *userArray  = [NSArray  arrayWithObjects:self.mobileTextField.text, self.passwordTextField.text, nil ];
            [MGAccountTool saveAccount:userArray];
            NSString * uid = [NSString stringWithFormat:@"%@",responseObj[@"data"][@"uid"]];
            [MGAccountTool saveUid:uid];
            
            CATransition * anim = [CATransition animation];
            anim.type = @"kCATransitionMoveIn";
            anim.duration = 0.5;
            [[UIApplication sharedApplication].keyWindow.layer addAnimation:anim forKey:nil];
            
            HMHomeViewController * vc = [[HMHomeViewController alloc] init];
            [UIApplication sharedApplication].keyWindow.rootViewController = vc;

            
            
            
        } else {
            [HMHUDManager showHUDWithText:@"账号或者密码不正确"];
        }
        
    } failure:^(NSError *error) {
        
    }];
    
}


// 忘记密码
- (IBAction)forgetPasswordBtnClickedHandler:(id)sender { // 忘记密码
    HMForgetController * vc = [[HMForgetController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

// 检测表单
- (BOOL)checkForm
{
    BOOL loginEnabled = YES;
    NSUInteger passwordLength = self.passwordTextField.text.length;
    NSString * errorMsg;
    
    if (passwordLength == 0) {
        errorMsg = @"请输入密码";
        loginEnabled = NO;
    } else if (passwordLength < 6) {
        errorMsg = @"密码长度不得小于6位";
        loginEnabled = NO;
    } else if (passwordLength > 16) {
        errorMsg = @"密码长度不得大于16位";
        loginEnabled = NO;
    }
    if(!loginEnabled && [errorMsg length] > 0){
        [HMHUDManager showHUDWithText:errorMsg];
    }
    return loginEnabled;
}

- (void)setupView
{
    [self configTextField:_mobileTextField imageName:@"phone" width:38 height:29 placeHolder:@"请输入手机号码"];
    [self configTextField:_passwordTextField imageName:@"lock" width:38 height:29 placeHolder:@"请输入密码"];
    [self configButton:_loginBtn borderWidth:0 borderColor:nil];
    [self configButton:_registerBtn borderWidth:1 borderColor:kMainColor];
    
    self.registerBtn.hidden = YES;
//    self.forgetBtn.hidden = YES;
}

- (void)configTextField:(UITextField *)textField imageName:(NSString *)imageName width:(CGFloat)width height:(CGFloat)height placeHolder:(NSString *)placeHolder
{
    textField.placeholder = placeHolder;
    textField.font = [UIFont systemFontOfSize:17];
    textField.textColor = kTextColor;
    textField.backgroundColor = [UIColor whiteColor];
    textField.clearButtonMode = UITextFieldViewModeAlways;
    
    UIImageView * leftView = [[UIImageView alloc] init];
    leftView.contentMode = UIViewContentModeScaleAspectFit;
    leftView.bounds = CGRectMake(0, 0, width, height);
    leftView.image = [UIImage imageNamed:imageName];
    textField.leftView = leftView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    
    textField.layer.cornerRadius = 5;
    textField.layer.masksToBounds = YES;
    textField.layer.shadowColor = [UIColor blackColor].CGColor;
    textField.layer.shadowOffset = CGSizeMake(4, 4);
    textField.layer.shadowRadius = 4;
    textField.layer.shadowOpacity = 0.8;
}

- (void)configButton:(UIButton *)button  borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor
{
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    button.layer.borderWidth = borderWidth;
    button.layer.borderColor = [borderColor CGColor];
}



@end
